#include <bits/stdc++.h>
using namespace std;
const int N=100005;
int T,n,m;
int h[N],e[N],ne[N],idx,w[N];

int dist[N][2];
bool st[N][2];
int dist1[N][2];
int s,f;
struct PII
{
	int id,type,dist;
	bool operator> (const PII& node) const {
	        return dist > node.dist;
	    }
	    
};
int add(int a,int b,int c)
{
	e[idx]=b;
	ne[idx]=h[a];
	w[idx]=c;
	h[a]=idx++;
	
}
int dijkstra(int s)
{
	memset(dist,0x3f,sizeof dist);
	memset(dist1,0x3f,sizeof dist1);
	priority_queue<PII,vector<PII>,greater<PII> >heap;
	dist[s][0]=0;
	dist1[s][0]=1;
	heap.push({s,0,0});
	
	while(heap.size())
	{
		auto t=heap.top();
		int ver=t.id;
		int type=t.type;
		heap.pop();
		if(st[ver][type]) continue;
		st[ver][type]=true;
		for(int i=h[ver];i!=-1;i=ne[i])
		{
			int j=e[i];
			if(dist[j][0]>dist[ver][type]+w[i])
			{
				dist[j][1]=dist[j][0];
				dist1[j][1]=dist1[j][0];
				heap.push({j,1,dist[j][1]});
                dist[j][0]=dist[ver][type]+w[i];
                dist1[j][0]=dist1[ver][type];
                heap.push({j,0,dist[j][0]});
			}
			else if(dist[j][0]==dist[ver][type]+w[i])
			{
				dist1[j][0]+=dist1[ver][type];
			}
			else if(dist[j][1]==dist[ver][type]+w[i])
			{
				dist1[j][1]+=dist1[ver][type];
			}
			else if(dist[j][1]>dist[ver][type]+w[i])
			{
				dist[j][1]=dist[ver][type]+w[i];
				dist1[j][1]=dist1[ver][type];
				heap.push({j,1,dist[j][1]});
			}
			
		}
	
	
	}
	
	
	int res=dist1[f][0];
		    if(dist[f][0]+1==dist[f][1]) res+=dist1[f][1];
		    return res;
	
}
int main()
{
	cin>>T;
	while(T--)
	{
		memset(h,-1,sizeof h);	
		memset(st,false,sizeof st);
		memset(e,0,sizeof e);
		memset(ne,0,sizeof ne);
		
		
	    cin>>n>>m;
      	while(m--)
	{
		int a,b,c;
		cin>>a>>b>>c;
		add(a,b,c);
	}
	    
	    cin>>s>>f;
	   cout<<dijkstra(s)<<endl;
	   
	   	    
	}
	return 0; 
	}
